﻿using System;
using System.Collections.Generic;
using System.Transactions;
using Common;
using Dto;
using SqlServerDAL;

namespace Business
{
    public class ChannelBusiness
    {
        private const string Waming = "Business.ChannelBusiness";
        private readonly AnnexBusiness annexBusiness = new AnnexBusiness();
        private readonly SiteChannelDAL siteChannelDal = new SiteChannelDAL();

        /// <summary>
        /// 保存站点频道
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="annexDto"></param>
        /// <returns></returns>
        public bool Save(SiteChannelDto dto, AnnexDto annexDto)
        {
            var tran = new TransactionScope();
            try
            {
                AnnexDto annex;
                if (annexDto != null)
                {
                    annex = new AnnexDto
                                {
                                    AnnexDate = annexDto.AnnexDate,
                                    AnnexHeight = annexDto.AnnexHeight,
                                    AnnexID = annexDto.AnnexID,
                                    AnnexName = annexDto.AnnexName,
                                    AnnexNarrowPath = annexDto.AnnexNarrowPath,
                                    AnnexOriginalPath = annexDto.AnnexOriginalPath,
                                    AnnexSize = annexDto.AnnexSize,
                                    AnnexWidth = annexDto.AnnexWidth,
                                    AnnexDomain = annexDto.AnnexDomain
                                };
                    annex = annexBusiness.AddAnnex(annex);
                    if (annex != null)
                    {
                        dto.ChannelIndexImg += annex.AnnexID + ",";
                    }
                }

                siteChannelDal.Save(dto);
                tran.Complete();
                return true;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".Save(SiteChannelDto dto)", ex);
                return false;
            }
            finally
            {
                tran.Dispose();
            }
        }

        /// <summary>
        /// 获取站点频道列表
        /// </summary>
        /// <param name="pageNo"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<SiteChannelDto> GetList(int pageNo, int pageSize, out int totalCount)
        {
            try
            {
                List<SiteChannelDto> dtos = null;
                var entityList = siteChannelDal.SlicedFindAll((pageNo - 1) * pageSize, pageSize, "");
                if (entityList != null && entityList.Count > 0)
                {
                    dtos = new List<SiteChannelDto>();
                    foreach (var entity in entityList)
                    {
                        var dto = FillDto.FillSiteChannelDto(entity);
                        dtos.Add(dto);
                    }
                }

                totalCount = siteChannelDal.SelectCount();
                return dtos;
            }
            catch (Exception ex)
            {
                totalCount = -1;
                Log.LogHandler.LogError(Waming + ".GetList(int pageNo, int pageSize, out int totalCount)", ex);
                return null;
            }
        }

        /// <summary>
        /// 通过ID获取站点频道
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public SiteChannelDto GetSiteChannelById(int id)
        {
            try
            {
                var entity = siteChannelDal.FindById(id);
                var dto = FillDto.FillSiteChannelDto(entity);
                return dto;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".GetSiteChannelById(int id)", ex);
                return null;
            }
        }

        /// <summary>
        /// 通过标志获取站点频道
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public SiteChannelDto GetSiteChannelByCode(string code)
        {
            try
            {
                var entity = siteChannelDal.FindOne(code);
                SiteChannelDto dto = FillDto.FillSiteChannelDto(entity);
                return dto;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".GetSiteChannelByCode(int id)", ex);
                return null;
            }
        }

        /// <summary>
        /// 获取正常显示的频道
        /// </summary>
        /// <returns></returns>
        public List<SiteChannelDto> GetShowList()
        {
            try
            {
                List<SiteChannelDto> dtos = null;
                var hts = siteChannelDal.FindAll(1);
                if (hts != null && hts.Count > 0)
                {
                    dtos = new List<SiteChannelDto>();
                    foreach (var ht in hts)
                    {
                        var dto = FillDto.FillSiteChannelDto(ht);
                        dtos.Add(dto);
                    }
                }
                return dtos;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".GetShowList()", ex);
                return null;
            }
        }

        /// <summary>
        /// 获取需要显示的图片详细
        /// </summary>
        /// <param name="imgs"></param>
        /// <returns></returns>
        public List<AnnexDto> GetIndexImgs(string imgs)
        {
            var annexDtos = new List<AnnexDto>();
            var showImgs = imgs.Split(new[] { ',' });
            foreach (var img in showImgs)
            {
                if (img == "") continue;
                var annexDto = annexBusiness.GetAnnexById(Convert.ToInt32(img));
                annexDtos.Add(annexDto);
            }
            return annexDtos;
        }
    }
}